//#condition MUJMAIL_SEARCH
/*
MujMail - Simple mail client for J2ME
Copyright (C) 2008 David Hauzar <david.hauzar.mujmail@gmail.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

package mujmail.search;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import javax.microedition.rms.RecordStore;

/**
 * Reminds whether given item was selected.
 * Saves the selected state to the RMS database.
 * 
 * @author David Hauzar
 */
public class WasSelectedReminder {
    private final String dbID;
    private boolean wasSelected;
    private boolean loaded = false;
    /** Used to create name of databases that stores the selected state. */
    public static final String DB_PREFIX = "wsr_";

    /**
     * Creates the instance of reminder.
     * @param dbID the RMS database id to that save the selected state.
     */
    public WasSelectedReminder(String dbID) {
        this.dbID = dbID;
    }



    /**
     * Reminds whether the item was selected in the past.
     * @return true if the item was selected
     *  false if the item was not selected
     */
    public boolean remindWasSelected() {
        if (!loaded) {
            loadWasSelected();
        }
        return wasSelected;
    }

    /**
     * Set the current state of the item. 
     * @param isSelected
     */
    public void setWasSelected(boolean isSelected) {
        wasSelected = isSelected;
        saveWasSelected();
    }

    /**
     * Save the selected to the persistent storage (RMS database).
     */
    private void saveWasSelected() {
        RecordStore rs = null;
        try {
            rs = RecordStore.openRecordStore(dbID, true);
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
            DataOutputStream stream = new DataOutputStream(buffer);

            stream.writeBoolean(wasSelected);
            stream.flush();
            if (rs.getNumRecords() == 1) {
                rs.setRecord(1, buffer.toByteArray(), 0, buffer.size());
            } else {
                rs.addRecord(buffer.toByteArray(), 0, buffer.size());
            }
        } catch (Exception e) {
        } finally {
            try {
                rs.closeRecordStore();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

    }

    /**
     * Load was selected from the persistent storage (RMS database).
     */
    private void loadWasSelected() {
        loaded = true;

        RecordStore rs = null;
        try {
            rs = RecordStore.openRecordStore(dbID, true);
            ByteArrayInputStream buffer = new ByteArrayInputStream(rs.getRecord(1));
            DataInputStream stream = new DataInputStream(buffer);

            wasSelected = stream.readBoolean();

        } catch (Exception e) {
            wasSelected = true;
        } finally {
            try {
                rs.closeRecordStore();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }



}
